Filter

The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.

syntax:

  • filter(function, sequence)

Parameters:

  • function: function that tests if each element of a 
  • sequence true or not.
  • sequence: sequence which needs to be filtered, it can 
  • be sets, lists, tuples, or containers of any iterators.

Returns:

returns an iterator that is already filtered. 

def fun(variable):
letters = ['a', 'e', 'i', 'o', 'u']
if (variable in letters):
return True
else:
return False

# sequence
sequence = ['g', 'e', 'e', 'j', 'k', 's', 'p', 'r']

# using filter function
filtered = filter(fun, sequence)

print('The filtered letters are:')
for s in filtered:
print(s)


# function that filters vowels
# a list contains both even and odd numbers.
seq = [0, 1, 2, 3, 5, 8, 13]

# result contains odd numbers of the list
result = filter(lambda x: x % 2 != 0, seq)
print(list(result))

# result contains even numbers of the list
result = filter(lambda x: x % 2 == 0, seq)
print(list(result))

No comments:

Post a Comment